<?php

class FeedbacksController extends Controller
{
	public $layout='//layouts/main';

	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

  public function actions(){
    return array(
        'captcha'=>array(
            'class'=>'CaptchaExtendedAction',
        ),
    );
  }

	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view', 'create', 'captcha'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionCreate()
	{
		$model=new Feedbacks('frontend');

		if(isset($_POST['Feedbacks']))
		{
			$model->attributes=$_POST['Feedbacks'];
			if($model->validate('frontend'))
      {
				$this->redirect("index?notif=success");
      }
      else
      {
        $this->render('index',array(
            'feedback' => $model,
        ));
      }
		}
	}

	public function actionIndex()
	{
    $feedback = new Feedbacks;
		$this->render('index', array(
            'feedback' => $feedback,
        ));
	}

	public function loadModel($id)
	{
		$model=Feedbacks::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='feedbacks-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
